package com.zhongshengnetwork.rightbe.wzt.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.BezierRadarHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhongshengnetwork.rightbe.Adv.model.AdvertUtils;
import com.zhongshengnetwork.rightbe.Adv.model.SPManageUtils;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.Constant.Constant;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.common.ScreenUtils;
import com.zhongshengnetwork.rightbe.common.TopBarView;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.common.banner.BannerView;
import com.zhongshengnetwork.rightbe.common.topgrid.activity.WZTGridActivity;
import com.zhongshengnetwork.rightbe.gsonmodel.CommonModel;
import com.zhongshengnetwork.rightbe.gsonmodel.WeiShuInfoDetailModel;
import com.zhongshengnetwork.rightbe.https.HttpCallBack;
import com.zhongshengnetwork.rightbe.https.HttpsUtils;
import com.zhongshengnetwork.rightbe.lang.activity.BookmarkCardActivity;
import com.zhongshengnetwork.rightbe.lang.activity.LangPublishActivity;
import com.zhongshengnetwork.rightbe.lang.model.BookmarkModel;
import com.zhongshengnetwork.rightbe.login.LoginActivity;
import com.zhongshengnetwork.rightbe.wzt.model.WZTUtils;
import com.zhongshengnetwork.rightbe.wzt.view.WZTLatestFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.zhongshengnetwork.rightbe.MainActivity.REQUEST_WZT_MENU;

public class WZTLatestActivity extends AppBaseActivity implements TopBarView.onTitleBarClickListener {

    private TopBarView topbar;

    private final int linecount = 3;
    public String userid = null;
    private int pageindex = 0;
    private static final int Records = 20;
    private boolean isHasMore = false;
    private List<WeiShuInfoDetailModel> list;
    private ListView listview;
    private MyAdapter adapter;
    private int imgW = 0;
    private boolean isGetting = false;
    private ImageView no_data_img;
    private RefreshLayout mRefreshLayout;

    private RelativeLayout search_top;
    private LinearLayout my_classify_layout;
    private TextView classify_text;
    private RelativeLayout lang_item_layout;

    //轮播推荐
    private float density = 0.0f;
    private int screenWidth = 0;
    private List<View> viewList;
    private List<WeiShuInfoDetailModel> bannerList = null;
    BannerView bannerView = null;
    private String bannerBgUrl = "https://image.daydayrise.net/textbg.jpg";

    private void calculateImageWidth(){
        int width = ScreenUtils.getScreenWidth(WZTLatestActivity.this);
        density = ScreenUtils.getScreenDensity(WZTLatestActivity.this);
        screenWidth = width;
        float density = ScreenUtils.getScreenDensity(WZTLatestActivity.this);
        imgW = (int) ((width-40*density)/linecount);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        calculateImageWidth();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        calculateImageWidth();
        setContentView(R.layout.activity_wztlatest);

        topbar = (TopBarView)findViewById(R.id.topbar);
        topbar.getRightView().setVisibility(View.GONE);
        topbar.setClickListener(this);

        pageindex = 0;

        if (list == null){
            list = new ArrayList<WeiShuInfoDetailModel>();
        }
        no_data_img = (ImageView)findViewById(R.id.no_data_img);
        listview=(ListView) findViewById(R.id.wzt_listview);
        adapter=new MyAdapter();
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("TAG","position="+position);
            }
        });

        mRefreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        //设置 Header 为 贝塞尔雷达 样式
        mRefreshLayout.setRefreshHeader(new BezierRadarHeader(WZTLatestActivity.this).setEnableHorizontalDrag(true));//BezierRadarHeader
        //设置 Footer 为 球脉冲 样式
        mRefreshLayout.setRefreshFooter(new ClassicsFooter(WZTLatestActivity.this).setSpinnerStyle(SpinnerStyle.FixedBehind));//BallPulseFooter ClassicsFooter
        mRefreshLayout.setPrimaryColors(AppThemeUtils.getInstance().getRefreshBackgroundColor(),AppThemeUtils.getInstance().getRefreshAccessColor());
        mRefreshLayout.setEnableRefresh(true);//启用刷新
        mRefreshLayout.setEnableLoadMore(false);//启用加载
        //刷新
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                //refreshlayout.finishRefresh();
                pageindex = 0;
                getData();
            }
        });
        mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //refreshLayout.finishLoadMore();
                getData();
            }
        });

        search_top = (RelativeLayout) findViewById(R.id.search_top);
        //search_top.setVisibility(View.GONE);
        search_top.setClickable(true);
        search_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (WZTLatestActivity.this == null){
                    return;
                }
                Intent intent = new Intent(WZTLatestActivity.this, WZTSearchActivity.class);
                startActivity(intent);
            }
        });

        my_classify_layout = (LinearLayout) findViewById(R.id.my_classify_layout);
        my_classify_layout.setVisibility(View.VISIBLE);
        my_classify_layout.setClickable(true);
        my_classify_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(CommonUtils.isLogin() == false){
                    Intent intent = new Intent(WZTLatestActivity.this, LoginActivity.class);
                    startActivity(intent);
                    return;
                }

                //频道选择
                Intent intent = new Intent(WZTLatestActivity.this, WZTGridActivity.class);
                startActivityForResult(intent, REQUEST_WZT_MENU);

            }
        });

        LinearLayout wzt_layout = (LinearLayout) findViewById(R.id.wzt_layout);
        wzt_layout.setBackgroundColor(AppThemeUtils.getInstance().getBackgroundColor());
        listview.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
        listview.setDivider(new ColorDrawable(AppThemeUtils.getInstance().getMainColor()));
        listview.setDividerHeight(AppThemeUtils.DividerHeight);

        LinearLayout top_layout = (LinearLayout) findViewById(R.id.top_layout);
        top_layout.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
        search_top.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
        RelativeLayout search_shape_layout = (RelativeLayout) findViewById(R.id.search_shape_layout);
        search_shape_layout.setBackgroundResource(AppThemeUtils.getInstance().getSearchBackground());
        my_classify_layout.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
        classify_text = (TextView) findViewById(R.id.classify_text);
        classify_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());

        getData();

    }

    /**
     * 点击了返回导航按钮
     */
    @Override
    public void onBackClick() {
        finish();
    }

    /**
     * 点击了右边导航按钮
     */
    @Override
    public void onRightClick() {

    }

    public void getFirstData(boolean isFirst){
        if (listview == null || list == null){
            return;
        }
        if (list.size() == 0){
            //listview.startRefresh();
            if (mRefreshLayout != null){
                pageindex = 0;
                mRefreshLayout.autoRefresh();
            }
        }else {
            if (isFirst == true){
                //listview.smoothScrollToPosition(0);
                listview.setSelection(0);
            }
        }
    }

    public void updateData(){
        if (listview == null || list == null){
            return;
        }
        if (mRefreshLayout != null){
            pageindex = 0;
            mRefreshLayout.autoRefresh();
        }
    }

    //获取自定义的数据
    private String getDataId(){
        if (CommonUtils.isLogin() == true){
            String my = SPManageUtils.getInstance(CustomApplication.mContext).getSPString(AdvertUtils.MyWZTChannelDataKey+CustomApplication.loginModel.getUid(), "");
            if (!CommonUtils.isEmpty(my)){
                String newData = SPManageUtils.getInstance(CustomApplication.mContext).getSPString(AdvertUtils.NewWZTChannelDataKey, "");
                if (!CommonUtils.isEmpty(newData)){
                    List<String> mList = CommonUtils.stringToArrayList(my);
                    List<String> newList = CommonUtils.stringToArrayList(newData);
                    List<String> dList = new ArrayList<String>();
                    dList.addAll(mList);
                    for (int i = 0; i < newList.size(); i++){
                        if (dList.contains(newList.get(i)) == false){
                            dList.add(newList.get(i));
                        }
                    }
                    return ""+CommonUtils.listToString(dList)+"";
                }
            }
            if (!CommonUtils.isEmpty(my)){
                return ""+my+"";
            }
        }
        return "";
    }

    private void getData(){
        if (listview == null){
            return;
        }
        if (isGetting == true){
            if (mRefreshLayout.getState() == RefreshState.Refreshing){
                mRefreshLayout.finishRefresh();
            }else {
                mRefreshLayout.finishLoadMore();
            }
            return;
        }
        isGetting = true;
        new Thread(){
            @Override
            public void run() {
                super.run();
                if (pageindex == 0){
                    updateBanner();
                }
                final HashMap<String, String> map = new HashMap<String, String>();
                String url = "weishu/theme/latest/list.do";
                map.put(APIKey.tokenKey, CommonUtils.formatString(CustomApplication.loginModel.getToken()));
                map.put("dataId", getDataId());
                map.put("dataType","100");
                map.put("pageIndex",pageindex+"");
                map.put("pageRecord",Records+"");
                HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        Log.e("微纸条",data+"\n");
                        isGetting = false;
                        if (WZTLatestActivity.this == null){
                            return;
                        }
                        CommonModel commonModel = GsonTools.getCommonModel(data);
                        if (commonModel.getFlag().equals("1")){
                            List<WeiShuInfoDetailModel> l = GsonTools.getWeiShuInfoDetailModel(data);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (mRefreshLayout.getState() == RefreshState.Refreshing){
                                        mRefreshLayout.finishRefresh();
                                        list.clear();
                                    }else {
                                        mRefreshLayout.finishLoadMore();
                                    }
                                    if (l != null && l.size() > 0){
                                        no_data_img.setVisibility(View.INVISIBLE);
                                        if (l.size() >= Records){
                                            //listview.setPullLoadEnable(true);
                                            mRefreshLayout.setEnableLoadMore(true);
                                        }else {
                                            //listview.setPullLoadEnable(false);
                                            mRefreshLayout.setEnableLoadMore(false);
                                        }
                                        list.addAll(l);
                                        //Log.e("微纸条","数据大小="+list.size()+"\n");
                                    }else {
                                        isHasMore = false;
                                        //listview.setPullLoadEnable(false);
                                        mRefreshLayout.setEnableLoadMore(false);
                                    }
                                    adapter.notifyDataSetChanged();
                                    if (pageindex == 0){
                                        //listview.smoothScrollToPosition(0);
                                        listview.setSelection(0);
                                    }
                                    if (l != null && l.size() > 0){
                                        pageindex += 1;
                                    }
                                }
                            });
                        }else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (mRefreshLayout.getState() == RefreshState.Refreshing){
                                        mRefreshLayout.finishRefresh();
                                        list.clear();
                                    }else {
                                        mRefreshLayout.finishLoadMore();
                                    }
                                    CustomApplication.showTip(commonModel, WZTLatestActivity.this);
                                }
                            });
                        }
                    }

                    @Override
                    public void onError(String msg) {
                        isGetting = false;
                        if (WZTLatestActivity.this != null){
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (mRefreshLayout.getState() == RefreshState.Refreshing){
                                        mRefreshLayout.finishRefresh();
                                    }else {
                                        mRefreshLayout.finishLoadMore();
                                    }
                                }
                            });
                        }
                    }
                });
            }
        }.start();

    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            int count = list.size()/linecount;
            if (list.size()%linecount > 0){
                count += 1;
            }
            return count+1;
        }


        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public boolean isEnabled(int position) {
            // TODO Auto-generated method stub
            return super.isEnabled(position);
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public int getItemViewType(int position) {
            if (position == 0){
                return 0;
            }
            return 1;
        }

        /**
         * @param position
         * @param convertView
         * @param parent
         * @return
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View view = null;
            int itype = getItemViewType(position);
            if (itype == 0){
                if (convertView == null){
                    view = View.inflate(WZTLatestActivity.this, R.layout.banner_item_layout,null);
                }else {
                    view = convertView;
                }
                bannerView = (BannerView) view.findViewById(R.id.banner);
                RelativeLayout banner_item_layout = (RelativeLayout) view.findViewById(R.id.banner_item_layout);
                //设置主题下的颜色
                banner_item_layout.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
                bannerView.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
                bannerView.startLoop(true);
                ViewGroup.LayoutParams params = bannerView.getLayoutParams();
                params.width = (int)(screenWidth - density*20);
                params.height = (int)((screenWidth - density*20)/3);
                Log.e("TAG", "--话题："+params.width+"——"+params.height);
                bannerView.setLayoutParams(params);
                if (viewList != null && viewList.size() > 0){
                    bannerView.setViewList(viewList);
                }
                return view;
            }

            //convertView的复用
            if (convertView == null) {
                convertView = View.inflate(WZTLatestActivity.this, R.layout.wzt_listview_item,null);
            }
            view=convertView;

            LinearLayout wzt_layout_1 = (LinearLayout) view.findViewById(R.id.wzt_layout_1);
            LinearLayout wzt_layout_2 = (LinearLayout) view.findViewById(R.id.wzt_layout_2);
            LinearLayout wzt_layout_3 = (LinearLayout) view.findViewById(R.id.wzt_layout_3);

            ImageView wzt_img1 = (ImageView) view.findViewById(R.id.wzt_img1);
            TextView wzt_readcount_1 = (TextView) view.findViewById(R.id.wzt_readcount_1);
            TextView private_text_1 = (TextView) view.findViewById(R.id.private_text_1);
            TextView wzt_text1 = (TextView) view.findViewById(R.id.wzt_text1);


            ImageView wzt_img2 = (ImageView) view.findViewById(R.id.wzt_img2);
            TextView wzt_readcount_2 = (TextView) view.findViewById(R.id.wzt_readcount_2);
            TextView private_text_2 = (TextView) view.findViewById(R.id.private_text_2);
            TextView wzt_text2 = (TextView) view.findViewById(R.id.wzt_text2);

            ImageView wzt_img3 = (ImageView) view.findViewById(R.id.wzt_img3);
            TextView wzt_readcount_3 = (TextView) view.findViewById(R.id.wzt_readcount_3);
            TextView private_text_3 = (TextView) view.findViewById(R.id.private_text_3);
            TextView wzt_text3 = (TextView) view.findViewById(R.id.wzt_text3);


            wzt_img1.setVisibility(View.GONE);
            wzt_layout_1.setVisibility(View.GONE);
            wzt_readcount_1.setVisibility(View.GONE);
            private_text_1.setVisibility(View.GONE);
            wzt_text1.setVisibility(View.GONE);


            wzt_img2.setVisibility(View.GONE);
            wzt_layout_2.setVisibility(View.GONE);
            wzt_readcount_2.setVisibility(View.GONE);
            private_text_2.setVisibility(View.GONE);
            wzt_text2.setVisibility(View.GONE);

            wzt_img3.setVisibility(View.GONE);
            wzt_layout_3.setVisibility(View.GONE);
            wzt_readcount_3.setVisibility(View.GONE);
            private_text_3.setVisibility(View.GONE);
            wzt_text3.setVisibility(View.GONE);

            LinearLayout wzt_listview_item_layout = (LinearLayout) view.findViewById(R.id.wzt_listview_item_layout);
            wzt_listview_item_layout.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
            wzt_text1.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
            wzt_text2.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
            wzt_text3.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
            wzt_img1.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
            wzt_img2.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
            wzt_img3.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());

            Log.e("微纸条","position="+position+"\n");
            position -= 1;
            int num = 0;
            int count = linecount*position;
            if (list.size()-count >= linecount){
                num = linecount;
            }else {
                while (list.size()-count > 0){
                    num+=1;
                    count+=1;
                }
            }
            int index = linecount*position;
            RequestOptions options = new RequestOptions().error(AppThemeUtils.getInstance().getDefaultImageBg()).bitmapTransform(new RoundedCorners(Constant.CornerRadius));//图片圆角
            for (int i = 0; i < num; i++){
                WeiShuInfoDetailModel model = list.get(index+i);
                Log.e("微纸条","标题="+model.getTitle()+"\n");
                if (i == 0){
                    wzt_img1.setVisibility(View.VISIBLE);
                    wzt_layout_1.setVisibility(View.VISIBLE);
                    wzt_readcount_1.setVisibility(View.VISIBLE);
                    wzt_text1.setVisibility(View.VISIBLE);
                    ViewGroup.LayoutParams params = wzt_img1.getLayoutParams();
                    params.width =imgW;
                    params.height = imgW;
                    wzt_img1.setLayoutParams(params);
                    params = wzt_text1.getLayoutParams();
                    params.width = imgW;
                    wzt_text1.setLayoutParams(params);
                    if (WZTLatestActivity.this != null){
                        try {
                            Glide.with(WZTLatestActivity.this).load(model.getCover()).apply(options).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(wzt_img1);
                        }catch (Exception e){
                            LangPublishActivity.uploadError("从首页进入微纸条报错："+e.getLocalizedMessage());
                        }
                    }
                    wzt_text1.setText(model.getTitle());
                    wzt_readcount_1.setText(CommonUtils.getDisplayCount(model.getRealcount()));
                    if (model.getVisible() == false){
                        private_text_1.setVisibility(View.VISIBLE);
                    }

                    wzt_layout_1.setTag(index+i);
                    wzt_layout_1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onClickItem(v);
                        }
                    });

                }else if (i == 1){
                    wzt_img2.setVisibility(View.VISIBLE);
                    wzt_layout_2.setVisibility(View.VISIBLE);
                    wzt_readcount_2.setVisibility(View.VISIBLE);
                    wzt_text2.setVisibility(View.VISIBLE);
                    ViewGroup.LayoutParams params = wzt_img2.getLayoutParams();
                    params.width =imgW;
                    params.height = imgW;
                    wzt_img2.setLayoutParams(params);
                    params = wzt_text2.getLayoutParams();
                    params.width = imgW;
                    wzt_text2.setLayoutParams(params);
                    if (WZTLatestActivity.this != null){
                        try {
                            Glide.with(WZTLatestActivity.this).load(model.getCover()).apply(options).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(wzt_img2);
                        }catch (Exception e){
                            LangPublishActivity.uploadError("从首页进入微纸条报错："+e.getLocalizedMessage());
                        }
                    }
                    wzt_text2.setText(model.getTitle());
                    wzt_readcount_2.setText(CommonUtils.getDisplayCount(model.getRealcount()));
                    if (model.getVisible() == false){
                        private_text_2.setVisibility(View.VISIBLE);
                    }
                    wzt_layout_2.setTag(index+i);
                    wzt_layout_2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onClickItem(v);
                        }
                    });
                }else if (i == 2){
                    wzt_img3.setVisibility(View.VISIBLE);
                    wzt_layout_3.setVisibility(View.VISIBLE);
                    wzt_readcount_3.setVisibility(View.VISIBLE);
                    wzt_text3.setVisibility(View.VISIBLE);
                    ViewGroup.LayoutParams params = wzt_img3.getLayoutParams();
                    params.width =imgW;
                    params.height = imgW;
                    wzt_img3.setLayoutParams(params);
                    params = wzt_text3.getLayoutParams();
                    params.width = imgW;
                    wzt_text3.setLayoutParams(params);
                    if (WZTLatestActivity.this != null){
                        try {
                            Glide.with(WZTLatestActivity.this).load(model.getCover()).apply(options).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(wzt_img3);
                        }catch (Exception e){
                            LangPublishActivity.uploadError("从首页进入微纸条报错："+e.getLocalizedMessage());
                        }
                    }
                    wzt_text3.setText(model.getTitle());
                    wzt_readcount_3.setText(CommonUtils.getDisplayCount(model.getRealcount()));
                    if (model.getVisible() == false){
                        private_text_3.setVisibility(View.VISIBLE);
                    }
                    wzt_layout_3.setTag(index+i);
                    wzt_layout_3.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onClickItem(v);
                        }
                    });
                }
            }
            return view;
        }
    }

    private void onClickItem(View v){

        int index = (int)v.getTag();
        if (list == null || index >= list.size()){
            return;
        }

        WeiShuInfoDetailModel model = list.get(index);
        Log.e("详情",model.getTitle()+"\n");
        //model.setBookmarkId(30);
        if (model.getBookmarkId() != null && model.getBookmarkId() > 0){//收藏夹
            BookmarkModel m = new BookmarkModel();
            m.setBookmarkName(model.getTitle());
            m.setBookmarkDetail(model.getDetail());
            m.setBookmarkId(model.getBookmarkId());
            m.setContentCount(model.getContentCount());
            m.setVisible(model.getVisible());
            m.setUserid(model.getUserid());//不允许编辑
            m.setHeader(model.getHeader());
            m.setNickname(model.getNickname());
            m.setIcon(model.getCover());
            m.setContentCount(model.getContentCount());
            Intent intent = new Intent(WZTLatestActivity.this , BookmarkCardActivity.class);
            Bundle bundle=new Bundle();
            bundle.putSerializable("model", m);//序列化
            intent.putExtras(bundle);//发送数据
            startActivity(intent);
            return;
        }
        WZTUtils.goToWZT(WZTLatestActivity.this, model);

    }

    /***************************轮播相关***************************/
    private void updateBanner(){
        //首次从缓存里面获取数据
        Log.e("TAG","首次从缓存里面获取数据Banner");
        if (bannerList == null){
            bannerList = new ArrayList<>();
        }
        if (bannerList.size() == 0){
            String data = SPManageUtils.getInstance(CustomApplication.mContext).getSPString(AdvertUtils.WZTBannerData,"");
            if (!CommonUtils.isEmpty(data)){
                List<WeiShuInfoDetailModel> l = GsonTools.getWeiShuInfoDetailModel(data);
                bannerBgUrl = GsonTools.getWZTBannerBg(data);
                if (l != null && l.size() > 0){
                    bannerList.addAll(l);
                }
                upateBannerUI();
            }
        }

        final HashMap<String, String> map = new HashMap<String, String>();
        if (CommonUtils.isLogin() == true){
            map.put("token",CommonUtils.formatString(CustomApplication.loginModel.getToken()));
        }
        String url = "weishu/theme/banner/list.do";
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
            @Override
            public void onSuccess(String data) {
                Log.e("TAG","轮播数据："+data);
                CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    List<WeiShuInfoDetailModel> l = GsonTools.getWeiShuInfoDetailModel(data);
                    bannerBgUrl = GsonTools.getWZTBannerBg(data);
                    if (l != null && l.size() > 0){
                        SPManageUtils.getInstance(CustomApplication.mContext).putSPString(AdvertUtils.WZTBannerData,data);
                        bannerList.clear();
                        bannerList.addAll(l);
                        upateBannerUI();
                    }
                }
            }

            @Override
            public void onError(String msg) {
                Log.e("TAG","轮播数据失败："+msg);
            }
        });

    }

    private void upateBannerUI(){
        if (WZTLatestActivity.this == null){
            return;
        }
        if (viewList == null){
            viewList = new ArrayList<View>();
        }
        if (bannerView == null){
            return;
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                viewList.clear();
                for (int i = 0; i < bannerList.size(); i++) {
                    if(WZTLatestActivity.this == null){
                        continue;
                    }
                    WeiShuInfoDetailModel model = bannerList.get(i);
                    RelativeLayout layout = new RelativeLayout(WZTLatestActivity.this);
                    layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                    layout.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());

                    ImageView image = new ImageView(WZTLatestActivity.this);
                    image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                    //设置显示格式
                    image.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    image.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
                    RequestOptions options = new RequestOptions().error(AppThemeUtils.getInstance().getDefaultImageBg()).bitmapTransform(new RoundedCorners(25));//图片圆角
                    if (CommonUtils.isEmpty(bannerBgUrl)){
                        if (WZTLatestActivity.this != null){
                            try {
                                Glide.with(WZTLatestActivity.this).load("http://image.daydayrise.net/wordbg181012.png").apply(options).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(image);
                            }catch (Exception e){
                                LangPublishActivity.uploadError("从首页进入微纸条报错："+e.getLocalizedMessage());
                            }
                        }
                    }else {
                        if (WZTLatestActivity.this != null){
                            try {
                                Glide.with(WZTLatestActivity.this).load(bannerBgUrl).apply(options).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(image);
                            }catch (Exception e){
                                LangPublishActivity.uploadError("从首页进入微纸条报错："+e.getLocalizedMessage());
                            }
                        }
                    }
                    layout.addView(image);

                    if (model.getType() == 0 || model.getType() == 2 || model.getType() == 4){
                        final TextView textView = new TextView(WZTLatestActivity.this);
                        ViewGroup.LayoutParams l = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                        textView.setLayoutParams(l);
                        textView.setTag(i);
                        textView.setTextSize(16);
                        textView.setText(CommonUtils.formatString(model.getTitle()));
                        textView.setGravity(Gravity.CENTER);
                        textView.setTextColor(Color.parseColor("#ffffff"));
                        textView.setBackgroundColor(Color.parseColor("#00000000"));
                        textView.setPadding((int)(22),(int)(45),(int)(22),(int)(45));
                        textView.setLineSpacing(10,1);
                        textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD_ITALIC));//加粗斜体
                        textView.getPaint().setFakeBoldText(true);//加粗
                        textView.setMaxLines(2);
                        textView.setEllipsize(TextUtils.TruncateAt.END);
                        layout.addView(textView);
                    }

                    layout.setTag(i);
                    layout.setClickable(true);
                    layout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.e("TAG","点击："+v.getTag());
                            onClickBanner((int)v.getTag());
                        }
                    });
                    viewList.add(layout);
                }
                bannerView.setViewList(viewList);
            }
        });
    }

    //处理点击事件
    private void onClickBanner(final int index){
        if (bannerList != null && bannerList.size() > index){
            WeiShuInfoDetailModel model = bannerList.get(index);
            WZTUtils.goToWZT(WZTLatestActivity.this, model);
        }
    }


    /***************************轮播相关***************************/


}
